<template>
    <div>
        <div>
            <el-button class="add-btn" icon="el-icon-circle-plus-outline" type="success" size="mini" @click="openAdd">
                添加友情链接
            </el-button>
        </div>
        <div>
            <el-table
                    v-loading="loading"
                    :data="list"
                    border
                    size="mini"
                    style="width: 100%">
                <el-table-column
                        fixed
                        prop="id"
                        label="ID"
                        align="center"
                        width="200">
                </el-table-column>
                <el-table-column align="logo" prop="avatar" label="logo" width="60">
                    <template slot-scope="scope">
                        <el-image
                                class="img"
                                :src="scope.row.logo"
                                :preview-src-list="[scope.row.logo]">
                        </el-image>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="对方网站名称"
                        align="center">
                    <template slot-scope="scope">
                        <a :href="scope.row.url" target="_blank">{{scope.row.name}}</a>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="state"
                        label="状态"
                        align="center"
                        width="120">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.state == '1'">正常</el-tag>
                        <el-tag type="danger" v-if="scope.row.state == '0'">已删除</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="sort"
                        label="排序"
                        align="center"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        label="创建时间"
                        align="center"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="updateTime"
                        label="更新时间"
                        align="center"
                        width="180">
                </el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        align="center"
                        width="150">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="handleEdit(scope.row.id)" size="small">编辑</el-button>
                        <el-button type="danger" v-if="scope.row.state !== '0'" @click="handleDelete(scope.row.id)"
                                   size="small">删除
                        </el-button>
                        <el-button type="danger" v-if="scope.row.state === '0'" disabled
                                   @click="handleDelete(scope.row.id)" size="small">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 子组件：添加或更新 -->
        <FriendEdit
                :title="edit.title"
                :visible="edit.visible"
                :formData="edit.formData"
                :remoteClose="remoteClose"/>
    </div>
</template>
<script>
    import {getFriendLinks, getFriendLink, deleteFriendLink} from '@/api/api'
    import FriendEdit from './Friend-Edit'

    export default {
        components: {
            FriendEdit
        },

        data() {
            return {
                loading: false,
                list: [], // 列表数组
                // 新增或编辑数据
                edit: {
                    title: '',
                    visible: false,
                    formData: {}
                }
            }
        },
        created() {
            this.fetchData()
        },
        methods: {
            // 打开添加轮播图窗口
            openAdd() {
                this.edit.visible = true
                this.edit.title = '添加友情链接'
                this.edit.formData = {}
            },
            // 关闭新增或编辑子窗口
            remoteClose() {
                this.edit.visible = false
                this.fetchData()
            },
            // 获取列表数据
            fetchData() {
                this.loading = true
                getFriendLinks().then(response => {
                    this.loading = false
                    if (response.code === 20000) {
                        this.list = response.data
                    }
                })
            },
            // 根据ID详情
            async handleEdit(id) {
                const response = await getFriendLink(id)
                this.edit.formData = response.data
                this.edit.visible = true
                this.edit.title = '编辑轮播图'
            },
            // 删除
            handleDelete(id) {
                console.log('id', id)
                this.$confirm('确定删除这条记录吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 发送删除请求
                    deleteFriendLink(id).then(response => {
                        // 刷新列表数据
                        this.fetchData()
                        // 处理响应结果提示
                        if (response.code === 20000) {
                            this.$message.success(response.message)
                        } else {
                            this.$message.error(response.message)
                        }
                    })
                }).catch(() => {
                    // 取消删除，不用理会
                })
            }
        }
    }
</script>
<style scoped>
    a {
        text-decoration: none;
    }

    .add-btn {
        margin-bottom: 10px;
    }

</style>